/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package Conexion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/**
 *
 * @author Owner
 */
public class AccesoADatos {

    public Connection conexion(){
        Connection conexion=null;
        try{
    Class.forName("com.mysql.jdbc.Driver");
    conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/tpare", "root", "root");
        }
        catch(Exception e){}

    return conexion;
    }

    public ArrayList cargarCombo(String consulta, String nombreAtributo){
        ArrayList a= new ArrayList();
        try{
        Statement s=this.conexion().createStatement();
        ResultSet rs1=s.executeQuery(consulta);
        while(rs1.next())
        {
            a.add(rs1.getString(nombreAtributo));
        }
        }
        catch(SQLException e){}
        return a;
    }

    public Object[] cargaComboDependiente(String nombreTabla, String idAtributo,String nombreAtributo){ //por la forma en q lo hicimos es un quilombo!
          Object[] a= null;
        try{
        Statement s=this.conexion().createStatement();
        ResultSet rs= s.executeQuery("select * from "+nombreTabla+" where nombreProvincia="+idAtributo);
        int cont=0;
        do{
        rs.next();
        cont++;

        }while(!rs.isLast());
        a = new Object[cont];
        ResultSet rs1=s.executeQuery("select * from "+nombreTabla+" where nombreProvincia="+idAtributo);
        for(int i = 0; i<cont; i++)
        {
            rs1.next();
            a[i]=rs1.getString(nombreAtributo);

        }}
        catch(SQLException e){}
        return a;

    }

    public boolean insertar(String consulta){
    try{
        Statement s=this.conexion().createStatement();
        s.execute(consulta);
        return true;
        }
        catch(SQLException e){
        return false;
        }

    }

    public ResultSet leer(String consulta){
    ResultSet rs=null;
        try{
        Statement s=this.conexion().createStatement();
        rs=s.executeQuery(consulta);
        return rs;
        }
        catch(SQLException e){

        }
    return rs;
    }


 }
    

